import java.util.Scanner;


public class 輸入分割效能研究 {
	
	public static final int CINBUF_SIZE = 256;
	public static int cinoff; // 緩衝區資料的開始位置
	public static int cinend; // 緩衝區資料的結束位置
	public static byte[] cinbuf = new byte[CINBUF_SIZE];

	/**
	 * 小璋流切 Token 方法
	 * @return
	 */
	public static String readToken() {
		byte b;
		int begin = -1;
		int tklen = 0;
		
		try {
			// 尋找起始點
			while(begin==-1&&cinend!=-1) {
				// 無資料時讀取新資料
				if(cinoff==cinend) {
					cinoff = 0;
					cinend = System.in.read(cinbuf);
				}
				
				while(cinoff<cinend) {
					b = cinbuf[cinoff++];
					if(b!=' '&&b!='\t'&&b!='\r'&&b!='\n') {
						begin = cinoff-1;
						break;
					}
				}
			}

			// 尋找結束點
			if(begin!=-1) {
				while(tklen==0) {
					// 無資料時讀取新資料
					if(cinoff==cinend) {
						if(cinoff==CINBUF_SIZE) {
							cinoff = cinend-begin;
							System.arraycopy(cinbuf,begin,cinbuf,0,cinoff);							
							begin = 0;
						}
						cinend = cinoff+System.in.read(cinbuf,cinoff,CINBUF_SIZE-cinoff);
					}

					while(cinoff<cinend) {
						b = cinbuf[cinoff++];
						if(b==' '||b=='\t'||b=='\r'||b=='\n') {
							tklen = cinoff-begin-1;
							break;
						}
					}
				}
			}
		} catch(Exception e) {
			return null;
		}

		if(tklen==0||cinend==-1) return null;
		return new String(cinbuf,begin,tklen);
	}
	
	/**
	 * 實驗時請用 java 輸入分割效能研究 < ../in.txt
	 * @param args
	 */
	public static void main(String[] args) {
		int i;
		long begin,elapsed;
		String token = null;
		
		// 實驗數據 109ms
		begin = System.currentTimeMillis();
		for(i=0;i<100000;i++) {
			token = readToken();
		}
		elapsed = System.currentTimeMillis()-begin;
		System.out.printf("小璋流token耗時 %dms\n",elapsed);
		System.out.println(token);
		
		// 實驗數據 359ms
		Scanner sc = new Scanner(System.in);
		begin = System.currentTimeMillis();
		for(i=0;i<100000;i++) {
			token = sc.next();
		}
		elapsed = System.currentTimeMillis()-begin;
		System.out.printf("Scanner耗時 %dms\n",elapsed);
		System.out.println(token);
	}

}
